<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>dimensionpickerrenderer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_dimensionpickerrenderer.js.html">dimensionpickerrenderer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2008 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview The default renderer for a goog.dom.DimensionPicker.  A
<a name="line17"></a> * dimension picker allows the user to visually select a row and column count.
<a name="line18"></a> * It looks like a palette but in order to minimize DOM load it is rendered.
<a name="line19"></a> * using CSS background tiling instead of as a grid of nodes.
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.ui.DimensionPickerRenderer&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.dom&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line27"></a>goog.require(&#39;goog.i18n.bidi&#39;);
<a name="line28"></a>goog.require(&#39;goog.style&#39;);
<a name="line29"></a>goog.require(&#39;goog.ui.ControlRenderer&#39;);
<a name="line30"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Default renderer for {@link goog.ui.DimensionPicker}s.  Renders the
<a name="line35"></a> * palette as two divs, one with the un-highlighted background, and one with the
<a name="line36"></a> * highlighted background.
<a name="line37"></a> *
<a name="line38"></a> * @constructor
<a name="line39"></a> * @extends {goog.ui.ControlRenderer}
<a name="line40"></a> */
<a name="line41"></a>goog.ui.DimensionPickerRenderer = function() {
<a name="line42"></a>  goog.ui.ControlRenderer.call(this);
<a name="line43"></a>};
<a name="line44"></a>goog.inherits(goog.ui.DimensionPickerRenderer, goog.ui.ControlRenderer);
<a name="line45"></a>goog.addSingletonGetter(goog.ui.DimensionPickerRenderer);
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>/**
<a name="line49"></a> * Default CSS class to be applied to the root element of components rendered
<a name="line50"></a> * by this renderer.
<a name="line51"></a> * @type {string}
<a name="line52"></a> */
<a name="line53"></a>goog.ui.DimensionPickerRenderer.CSS_CLASS = &#39;goog-dimension-picker&#39;;
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * Return the underlying div for the given outer element.
<a name="line58"></a> * @param {Element} element The root element.
<a name="line59"></a> * @return {Element} The underlying div.
<a name="line60"></a> * @private
<a name="line61"></a> */
<a name="line62"></a>goog.ui.DimensionPickerRenderer.prototype.getUnderlyingDiv_ = function(
<a name="line63"></a>    element) {
<a name="line64"></a>  return element.firstChild.childNodes[1];
<a name="line65"></a>};
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * Return the highlight div for the given outer element.
<a name="line70"></a> * @param {Element} element The root element.
<a name="line71"></a> * @return {Element} The highlight div.
<a name="line72"></a> * @private
<a name="line73"></a> */
<a name="line74"></a>goog.ui.DimensionPickerRenderer.prototype.getHighlightDiv_ = function(
<a name="line75"></a>    element) {
<a name="line76"></a>  return /** @type {Element} */ element.firstChild.lastChild;
<a name="line77"></a>};
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Return the status message div for the given outer element.
<a name="line82"></a> * @param {Element} element The root element.
<a name="line83"></a> * @return {Element} The status message div.
<a name="line84"></a> * @private
<a name="line85"></a> */
<a name="line86"></a>goog.ui.DimensionPickerRenderer.prototype.getStatusDiv_ = function(
<a name="line87"></a>    element) {
<a name="line88"></a>  return /** @type {Element} */ (element.lastChild);
<a name="line89"></a>};
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Return the invisible mouse catching div for the given outer element.
<a name="line94"></a> * @param {Element} element The root element.
<a name="line95"></a> * @return {Element} The invisible mouse catching div.
<a name="line96"></a> * @private
<a name="line97"></a> */
<a name="line98"></a>goog.ui.DimensionPickerRenderer.prototype.getMouseCatcher_ = function(
<a name="line99"></a>    element) {
<a name="line100"></a>  return /** @type {Element} */ element.firstChild.firstChild;
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Overrides {@link goog.ui.ControlRenderer#canDecorate} to allow decorating
<a name="line106"></a> * empty DIVs only.
<a name="line107"></a> * @param {Element} element The element to check.
<a name="line108"></a> * @return {boolean} Whether if the element is an empty div.
<a name="line109"></a> */
<a name="line110"></a>goog.ui.DimensionPickerRenderer.prototype.canDecorate = function(
<a name="line111"></a>    element) {
<a name="line112"></a>  return element.tagName == goog.dom.TagName.DIV &amp;&amp; !element.firstChild;
<a name="line113"></a>};
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/**
<a name="line117"></a> * Overrides {@link goog.ui.ControlRenderer#decorate} to decorate empty DIVs.
<a name="line118"></a> * @param {goog.ui.DimensionPicker} palette The palette object.
<a name="line119"></a> * @param {Element} element The element to decorate.
<a name="line120"></a> * @return {Element} The decorated element.
<a name="line121"></a> */
<a name="line122"></a>goog.ui.DimensionPickerRenderer.prototype.decorate = function(palette,
<a name="line123"></a>    element) {
<a name="line124"></a>  goog.ui.DimensionPickerRenderer.superClass_.decorate.call(this,
<a name="line125"></a>      palette, element);
<a name="line126"></a>
<a name="line127"></a>  this.addElementContents_(palette, element);
<a name="line128"></a>  this.updateSize(palette, element);
<a name="line129"></a>
<a name="line130"></a>  return element;
<a name="line131"></a>};
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Scales various elements in order to update the palette&#39;s size.
<a name="line136"></a> * @param {goog.ui.DimensionPicker} palette The palette object.
<a name="line137"></a> * @param {Element} element The element to set the style of.
<a name="line138"></a> */
<a name="line139"></a>goog.ui.DimensionPickerRenderer.prototype.updateSize =
<a name="line140"></a>    function(palette, element) {
<a name="line141"></a>  var size = palette.getSize();
<a name="line142"></a>
<a name="line143"></a>  element.style.width = size.width + &#39;em&#39;;
<a name="line144"></a>
<a name="line145"></a>  var underlyingDiv = this.getUnderlyingDiv_(element);
<a name="line146"></a>  underlyingDiv.style.width = size.width + &#39;em&#39;;
<a name="line147"></a>  underlyingDiv.style.height = size.height + &#39;em&#39;;
<a name="line148"></a>
<a name="line149"></a>  if (palette.isRightToLeft()) {
<a name="line150"></a>      this.adjustParentDirection_(palette, element);
<a name="line151"></a>  }
<a name="line152"></a>};
<a name="line153"></a>
<a name="line154"></a>
<a name="line155"></a>/**
<a name="line156"></a> * Adds the appropriate content elements to the given outer DIV.
<a name="line157"></a> * @param {goog.ui.DimensionPicker} palette The palette object.
<a name="line158"></a> * @param {Element} element The element to decorate.
<a name="line159"></a> * @private
<a name="line160"></a> */
<a name="line161"></a>goog.ui.DimensionPickerRenderer.prototype.addElementContents_ = function(
<a name="line162"></a>    palette, element) {
<a name="line163"></a>  // First we create a single div containing three stacked divs.  The bottom div
<a name="line164"></a>  // catches mouse events.  We can&#39;t use document level mouse move detection as
<a name="line165"></a>  // we could lose events to iframes.  This is especially important in Firefox 2
<a name="line166"></a>  // in which TrogEdit creates iframes. The middle div uses a css tiled
<a name="line167"></a>  // background image to represent deselected tiles.  The top div uses a
<a name="line168"></a>  // different css tiled background image to represent selected tiles.
<a name="line169"></a>  var mouseCatcherDiv = palette.getDomHelper().createDom(goog.dom.TagName.DIV,
<a name="line170"></a>      this.getCssClass() + &#39;-mousecatcher&#39;);
<a name="line171"></a>  var unhighlightedDiv = palette.getDomHelper().createDom(goog.dom.TagName.DIV,
<a name="line172"></a>    {
<a name="line173"></a>      &#39;class&#39;: this.getCssClass() + &#39;-unhighlighted&#39;,
<a name="line174"></a>      &#39;style&#39;: &#39;width:100%;height:100%&#39;
<a name="line175"></a>    });
<a name="line176"></a>  var highlightedDiv = palette.getDomHelper().createDom(goog.dom.TagName.DIV,
<a name="line177"></a>      this.getCssClass() + &#39;-highlighted&#39;);
<a name="line178"></a>  element.appendChild(
<a name="line179"></a>      palette.getDomHelper().createDom(goog.dom.TagName.DIV,
<a name="line180"></a>          {&#39;style&#39;: &#39;width:100%;height:100%&#39;},
<a name="line181"></a>          mouseCatcherDiv, unhighlightedDiv, highlightedDiv));
<a name="line182"></a>
<a name="line183"></a>  // Lastly we add a div to store the text version of the current state.
<a name="line184"></a>  element.appendChild(palette.getDomHelper().createDom(goog.dom.TagName.DIV,
<a name="line185"></a>      this.getCssClass() + &#39;-status&#39;,
<a name="line186"></a>      goog.i18n.bidi.enforceLtrInText(&#39;0 x 0&#39;)));
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>/**
<a name="line191"></a> * Creates a div and adds the appropriate contents to it.
<a name="line192"></a> * @param {goog.ui.DimensionPicker} palette Palette to render.
<a name="line193"></a> * @return {Element} Root element for the palette.
<a name="line194"></a> */
<a name="line195"></a>goog.ui.DimensionPickerRenderer.prototype.createDom = function(palette) {
<a name="line196"></a>  var classNames = this.getClassNames(palette);
<a name="line197"></a>  var element = palette.getDomHelper().createDom(goog.dom.TagName.DIV, {
<a name="line198"></a>    &#39;class&#39; : classNames ? classNames.join(&#39; &#39;) : &#39;&#39;
<a name="line199"></a>  });
<a name="line200"></a>  this.addElementContents_(palette, element);
<a name="line201"></a>  this.updateSize(palette, element);
<a name="line202"></a>  return element;
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Initializes the control&#39;s DOM when the control enters the document.  Called
<a name="line208"></a> * from {@link goog.ui.Control#enterDocument}.
<a name="line209"></a> * @param {goog.ui.DimensionPicker} palette Palette whose DOM is to be
<a name="line210"></a> *     initialized as it enters the document.
<a name="line211"></a> */
<a name="line212"></a>goog.ui.DimensionPickerRenderer.prototype.initializeDom = function(
<a name="line213"></a>    palette) {
<a name="line214"></a>  goog.ui.DimensionPickerRenderer.superClass_.initializeDom.call(this, palette);
<a name="line215"></a>  this.positionMouseCatcher(palette);
<a name="line216"></a>};
<a name="line217"></a>
<a name="line218"></a>
<a name="line219"></a>/**
<a name="line220"></a> * Get the element to listen for mouse move events on.
<a name="line221"></a> * @param {goog.ui.DimensionPicker} palette The palette to listen on.
<a name="line222"></a> * @return {Element} The element to listen for mouse move events on.
<a name="line223"></a> */
<a name="line224"></a>goog.ui.DimensionPickerRenderer.prototype.getMouseMoveElement = function(
<a name="line225"></a>    palette) {
<a name="line226"></a>  return /** @type {Element} */ (palette.getElement().firstChild);
<a name="line227"></a>};
<a name="line228"></a>
<a name="line229"></a>
<a name="line230"></a>/**
<a name="line231"></a> * Returns the x offset in to the grid for the given mouse x position.
<a name="line232"></a> * @param {goog.ui.DimensionPicker} palette The table size palette.
<a name="line233"></a> * @param {number} x The mouse event x position.
<a name="line234"></a> * @return {number} The x offset in to the grid.
<a name="line235"></a> */
<a name="line236"></a>goog.ui.DimensionPickerRenderer.prototype.getGridOffsetX = function(
<a name="line237"></a>    palette, x) {
<a name="line238"></a>  // TODO: Don&#39;t rely on magic 18 - measure each palette&#39;s em size.
<a name="line239"></a>  return Math.min(palette.maxColumns, Math.ceil(x / 18));
<a name="line240"></a>};
<a name="line241"></a>
<a name="line242"></a>
<a name="line243"></a>/**
<a name="line244"></a> * Returns the y offset in to the grid for the given mouse y position.
<a name="line245"></a> * @param {goog.ui.DimensionPicker} palette The table size palette.
<a name="line246"></a> * @param {number} y The mouse event y position.
<a name="line247"></a> * @return {number} The y offset in to the grid.
<a name="line248"></a> */
<a name="line249"></a>goog.ui.DimensionPickerRenderer.prototype.getGridOffsetY = function(
<a name="line250"></a>    palette, y) {
<a name="line251"></a>  return Math.min(palette.maxRows, Math.ceil(y / 18));
<a name="line252"></a>};
<a name="line253"></a>
<a name="line254"></a>
<a name="line255"></a>/**
<a name="line256"></a> * Sets the highlighted size.
<a name="line257"></a> * @param {goog.ui.DimensionPicker} palette The table size palette.
<a name="line258"></a> * @param {number} columns The number of columns to highlight.
<a name="line259"></a> * @param {number} rows The number of rows to highlight.
<a name="line260"></a> */
<a name="line261"></a>goog.ui.DimensionPickerRenderer.prototype.setHighlightedSize = function(
<a name="line262"></a>    palette, columns, rows) {
<a name="line263"></a>  var element = palette.getElement();
<a name="line264"></a>
<a name="line265"></a>  // Style the highlight div.
<a name="line266"></a>  var style = this.getHighlightDiv_(element).style;
<a name="line267"></a>  style.width = columns + &#39;em&#39;;
<a name="line268"></a>  style.height = rows + &#39;em&#39;;
<a name="line269"></a>
<a name="line270"></a>  // Explicitly set style.right so the element grows to the left when increase
<a name="line271"></a>  // in width.
<a name="line272"></a>  if (palette.isRightToLeft()) {
<a name="line273"></a>    style.right = &#39;0&#39;;
<a name="line274"></a>  }
<a name="line275"></a>
<a name="line276"></a>  // Update the size text.
<a name="line277"></a>  goog.dom.setTextContent(this.getStatusDiv_(element),
<a name="line278"></a>      goog.i18n.bidi.enforceLtrInText(columns + &#39; x &#39; + rows));
<a name="line279"></a>};
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * Position the mouse catcher such that it receives mouse events past the
<a name="line284"></a> * selectedsize up to the maximum size.  Takes care to not introduce scrollbars.
<a name="line285"></a> * Should be called on enter document and when the window changes size.
<a name="line286"></a> * @param {goog.ui.DimensionPicker} palette The table size palette.
<a name="line287"></a> */
<a name="line288"></a>goog.ui.DimensionPickerRenderer.prototype.positionMouseCatcher = function(
<a name="line289"></a>    palette) {
<a name="line290"></a>  var mouseCatcher = this.getMouseCatcher_(palette.getElement());
<a name="line291"></a>  var doc = goog.dom.getOwnerDocument(mouseCatcher);
<a name="line292"></a>  var body = doc.body;
<a name="line293"></a>
<a name="line294"></a>  var position = goog.style.getRelativePosition(mouseCatcher, body);
<a name="line295"></a>
<a name="line296"></a>  // Hide the mouse catcher so it doesn&#39;t affect the body&#39;s scroll size.
<a name="line297"></a>  mouseCatcher.style.display = &#39;none&#39;;
<a name="line298"></a>
<a name="line299"></a>  // Compute the maximum size the catcher can be without introducing scrolling.
<a name="line300"></a>  var xAvailableEm = (palette.isRightToLeft() &amp;&amp; position.x &gt; 0) ?
<a name="line301"></a>      Math.floor(position.x / 18):
<a name="line302"></a>      Math.floor((body.scrollWidth - position.x) / 18);
<a name="line303"></a>
<a name="line304"></a>  // Computing available height is more complicated - we need to check the
<a name="line305"></a>  // window&#39;s inner height.
<a name="line306"></a>  var height;
<a name="line307"></a>  if (goog.userAgent.IE) {
<a name="line308"></a>    // Offset 20px to make up for scrollbar size.
<a name="line309"></a>    height = goog.style.getClientViewportElement(body).scrollHeight - 20;
<a name="line310"></a>  } else {
<a name="line311"></a>    var win = goog.dom.getWindow(doc);
<a name="line312"></a>    // Offset 20px to make up for scrollbar size.
<a name="line313"></a>    height = Math.max(win.innerHeight, body.scrollHeight) - 20;
<a name="line314"></a>  }
<a name="line315"></a>  var yAvailableEm = Math.floor((height - position.y) / 18);
<a name="line316"></a>
<a name="line317"></a>  // Resize and display the mouse catcher.
<a name="line318"></a>  mouseCatcher.style.width = Math.min(palette.maxColumns, xAvailableEm) + &#39;em&#39;;
<a name="line319"></a>  mouseCatcher.style.height = Math.min(palette.maxRows, yAvailableEm) + &#39;em&#39;;
<a name="line320"></a>  mouseCatcher.style.display = &#39;&#39;;
<a name="line321"></a>
<a name="line322"></a>  // Explicitly set style.right so the mouse catcher is positioned on the left
<a name="line323"></a>  // side instead of right.
<a name="line324"></a>  if (palette.isRightToLeft()) {
<a name="line325"></a>    mouseCatcher.style.right = &#39;0&#39;;
<a name="line326"></a>  }
<a name="line327"></a>};
<a name="line328"></a>
<a name="line329"></a>
<a name="line330"></a>/**
<a name="line331"></a> * Returns the CSS class to be applied to the root element of components
<a name="line332"></a> * rendered using this renderer.
<a name="line333"></a> * @return {string} Renderer-specific CSS class.
<a name="line334"></a> */
<a name="line335"></a>goog.ui.DimensionPickerRenderer.prototype.getCssClass = function() {
<a name="line336"></a>  return goog.ui.DimensionPickerRenderer.CSS_CLASS;
<a name="line337"></a>};
<a name="line338"></a>
<a name="line339"></a>/**
<a name="line340"></a> * This function adjusts the positioning from &#39;left&#39; and &#39;top&#39; to &#39;right&#39; and
<a name="line341"></a> * &#39;top&#39; as appropriate for RTL control.  This is so when the dimensionpicker
<a name="line342"></a> * grow in width, the containing element grow to the left instead of right.
<a name="line343"></a> * This won&#39;t be necessary if goog.ui.SubMenu rendering code would position RTL
<a name="line344"></a> * control with &#39;right&#39; and &#39;top&#39;.
<a name="line345"></a> * @private
<a name="line346"></a> *
<a name="line347"></a> * @param {goog.ui.DimensionPicker} palette The palette object.
<a name="line348"></a> * @param {Element} element The palette&#39;s element.
<a name="line349"></a> */
<a name="line350"></a>goog.ui.DimensionPickerRenderer.prototype.adjustParentDirection_ =
<a name="line351"></a>  function(palette, element) {
<a name="line352"></a>  var parent = palette.getParent();
<a name="line353"></a>  if (parent) {
<a name="line354"></a>    var parentElement = parent.getElement();
<a name="line355"></a>
<a name="line356"></a>    // Anchors the containing element to the right so it grows to the left
<a name="line357"></a>    // when it increase in width.
<a name="line358"></a>    var right = goog.style.getStyle(parentElement, &#39;right&#39;);
<a name="line359"></a>    if (right == &#39;&#39;) {
<a name="line360"></a>      var parentPos = goog.style.getPosition(parentElement);
<a name="line361"></a>      var parentSize = goog.style.getSize(parentElement);
<a name="line362"></a>      if (parentSize.width != 0 &amp;&amp; parentPos.x != 0) {
<a name="line363"></a>        var visibleRect = goog.style.getBounds(
<a name="line364"></a>            goog.style.getClientViewportElement());
<a name="line365"></a>        var visibleWidth = visibleRect.width;
<a name="line366"></a>        right = visibleWidth - parentPos.x - parentSize.width;
<a name="line367"></a>        goog.style.setStyle(parentElement, &#39;right&#39;, right + &#39;px&#39;);
<a name="line368"></a>      }
<a name="line369"></a>    }
<a name="line370"></a>
<a name="line371"></a>    // When a table is inserted, the containing elemet&#39;s position is
<a name="line372"></a>    // recalculated the next time it shows, set left back to &#39;&#39; to prevent
<a name="line373"></a>    // extra white space on the left.
<a name="line374"></a>    var left = goog.style.getStyle(parentElement, &#39;left&#39;);
<a name="line375"></a>    if (left != &#39;&#39;) {
<a name="line376"></a>      goog.style.setStyle(parentElement, &#39;left&#39;, &#39;&#39;);
<a name="line377"></a>    }
<a name="line378"></a>  } else {
<a name="line379"></a>    goog.style.setStyle(element, &#39;right&#39;, &#39;0px&#39;);
<a name="line380"></a>  }
<a name="line381"></a>};
</pre>


</body>
</html>
